// packageSquare/FindNeighbors/index.js
import { request } from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hotNeighbors: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  like(item) {
    if (item.currentTarget.dataset.item.isMark) {
      request({
        url: '/mini/api/findNeighbor/unLiked',
        method: 'POST',
        data: {
          userId: Number(
            item.currentTarget.dataset.item.userId
          )
        }
      }).then((resdata) => {
        if (resdata.code == 200) {
          wx.showToast({
            title: '取消关注成功',
            icon: 'none'
          })
          this.getHotNeighbors()
        } else {
          wx.showToast({
            title: resdata.message,
            icon: 'none'
          })
        }
      })
    } else {
      request({
        url: '/mini/api/findNeighbor/liked',
        method: 'POST',
        data: {
          userId: Number(
            item.currentTarget.dataset.item.userId
          )
        }
      }).then((resdata) => {
        if (resdata.code == 200) {
          wx.showToast({
            title: '成功',
            icon: 'none'
          })
          this.getHotNeighbors()
        } else {
          wx.showToast({
            title: resdata.message,
            icon: 'none'
          })
        }
      })
    }
  },

  share(detail) {
    let neighborDetail =
      detail.currentTarget.dataset.detail
    let data = {
      img: neighborDetail.themeImage,
      name: neighborDetail.name,
      address: location,
      date: neighborDetail.location
    }
    wx.navigateTo({
      url:
        '/packagePlan/share/share?data=' +
        JSON.stringify(data) +
        '&from=' +
        'neighbor'
    })
  },

  getHotNeighbors() {
    request({
      url:
        '/mini/api/findNeighbor/neighborSuggestions'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          hotNeighbors: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toHomePage(item) {
    wx.navigateTo({
      url:
        '/packageMy/personalHomepage/personalHomepage?userId=' +
        item.currentTarget.dataset.userid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getHotNeighbors()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
